﻿/*!	@file	r_score_server.cpp
	@brief	スコア管理
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "r_score_server.h"
#include "r_score_client.h"

#include "../r_debug.h"

// >>>>>>>>>> RTTI >>>>>>>>>>
#include "../rtti/r_rtti_reflection_implement.h"
RRTTI_REFLECTION_IMPLEMENT ( cRScoreServer, r::rtti::ClassBase,
							 RRTTI_REFLECTION_IMPLEMENT_TYPE ( _isUpdatable ),
							 RRTTI_REFLECTION_IMPLEMENT_TYPE ( _updateCount, _RST("スコア実行フレーム"), eRRTTIReflectionFlag_NoEdit ),
							 RRTTI_REFLECTION_IMPLEMENT_TYPE ( _currentScore, _RST("現在のスコア"), eRRTTIReflectionFlag_NoEdit ),
							 RRTTI_REFLECTION_IMPLEMENT_TYPE ( _nextScore, _RST("次回投入スコア"), eRRTTIReflectionFlag_NoEdit ),
							 RRTTI_REFLECTION_IMPLEMENT_TYPE ( _isLoading, _RST("スコアロード"), eRRTTIReflectionFlag_NoEdit ),
						   )

// <<<<<<<<<< RTTI <<<<<<<<<<

RSINGLETONGET_IMPLEMENT ( cRScoreServer, cRScoreServer )

cRScoreServer :: cRScoreServer()
: _isUpdatable(true)
, _updateCount(0)
, _currentScore(NULL)
, _nextScore(NULL)
, _isLoading(false)
{
}

cRScoreServer :: ~cRScoreServer()
{
}

void	cRScoreServer :: update()
{
	// 次のスコアがつまれた場合はそちらを先に消化
	if( _nextScore != NULL ) {
		// 一時凌ぎにフラグの使いまわし
		if( _isLoading == false ) {
			if( _currentScore != NULL ) {
				_currentScore->destroy();
			}
			_isLoading		= true;
		} else {
			delete _currentScore;
			_currentScore	= _nextScore;
			_currentScore->init();
			_nextScore		= NULL;
		}
		return;
	}
	// 現在のスコアを更新更新
	if ( _currentScore != NULL ) {
		if( _isLoading != false ) {
			_isLoading = !_currentScore->checkLoad();
		} else if ( _isUpdatable != false ) {
			_currentScore->update();
			++_updateCount;
		}
	}
}

void	cRScoreServer :: jumpScore( cRScoreClient* jumpScore )
{
	RDEBUG_ASSERT( jumpScore );
	RDEBUG_FINE( _RST("JumpTo->%s\n"), r::rtti::getRTTI( jumpScore )->getClassName() );
	_nextScore = r::rtti::dynamicCast<cRScoreClient*>( jumpScore );
	_isLoading = false;
}

void	cRScoreServer :: setUpdatable(r_bool enable)
{
	_updateCount = enable;
}



